package com.douwong.student.view;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.douwong.student.R;


public class CharPopuwindow extends PopupWindow {

	private TextView pingy;
	private TextView english;
	private TextView math;
	private GridView char_view;
	private CharAdapter adapter;
	private Context context;
	private String[]temp;
	private View view;
	private int[]charTitles={R.id.pingy,R.id.english,R.id.math};
	String pings[] = { "ā", "á", "ǎ", "à", "ē", "é", "ě", "è", "ī", "í", "ǐ", "ì", "ō", "ó", "ǒ", "ò", "ū", "ú", "ǔ",
			"ù", "ǖ", "ǘ", "ǚ", "ǜ", "ü" };
	String englishs[]={"i:","i","e","æ","ʌ","ə:","ə","u:","u","ɔ:","ɔ","a:","ei",
			"ai","ɔi","əu","au","iə","εə","uə","p","t","k","b","d","g",
			"f","s","ʃ","θ","h","v","z","ʒ","ð","tʃ","tr","ts","dʒ",
			"dr","dz","m","n","ŋ","l","r","w","j"};
	String []maths={"＋","－","±","×","÷","＝","≠","≡","≌","≈","＜ ","＞","≤","≥","％","∞","√"};
	public CharPopuwindow(Context context, final EditText editText) {
		this.context=context;
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		view = inflater.inflate(R.layout.char_popu, null);
		pingy = (TextView) view.findViewById(R.id.pingy);
		english = (TextView) view.findViewById(R.id.english);
		char_view = (GridView) view.findViewById(R.id.char_view);
		math=(TextView) view.findViewById(R.id.math);
		adapter=new CharAdapter();
		temp=pings;
		char_view.setAdapter(adapter);
			CharOnClick onClick=new CharOnClick();
			TextView text=(TextView) view.findViewById(charTitles[0]);
			text.setTextColor(context.getResources().getColor(R.color.c_00aeff));
			for(int i=0;i<charTitles.length;i++){
				view.findViewById(charTitles[i]).setOnClickListener(onClick);
			}
		
//		math.setOnClickListener(onClick);
//		pingy.setOnClickListener(onClick);
//		english.setOnClickListener(onClick);
		char_view.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				editText.append(temp[arg2]);
				dismiss();
			}
		});
		this.setContentView(view);
		this.setWidth(LayoutParams.WRAP_CONTENT);
		this.setHeight(LayoutParams.WRAP_CONTENT);
		// 设置SelectPicPopupWindow弹出窗体可点击
		this.setFocusable(true);
		ColorDrawable dw = new ColorDrawable(0x7fffffff);
		this.setBackgroundDrawable(dw);
	}
	private void stateChange(int id){
		for(int i=0;i<charTitles.length;i++){
			TextView text=(TextView) view.findViewById(charTitles[i]);
			text.setTextColor(context.getResources().getColor(R.color.c_000000));
		}
		TextView text=(TextView) view.findViewById(id);
		text.setTextColor(context.getResources().getColor(R.color.c_00aeff));
	}
    class CharOnClick implements OnClickListener{

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.pingy:
				temp=pings;
				adapter.notifyDataSetChanged();
				break;
			case R.id.english:
				temp=englishs;
				adapter.notifyDataSetChanged();
				break;
			case R.id.math:
				temp=maths;
				adapter.notifyDataSetChanged();
				break;
			default:
				break;
			}
			stateChange(v.getId());
		}
    	
    }
	class CharAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return temp.length;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			convertView=LayoutInflater.from(context).inflate(R.layout.char_item, null);
			TextView char_text=(TextView) convertView.findViewById(R.id.char_text);
			char_text.setText(temp[position]);
			return convertView;
		}

	}
}
